<!--收费标准管理-->
<script setup>
import {ref, onMounted, reactive} from 'vue'
import request from "@/utils/request.js";
import {ElMessage} from "element-plus";
import LoginView from "@/views/LoginView.vue";
const params = reactive({
  pageNum: 1,
  pageSize: 10,
  chargeType: ''
})
/*数据*/
const chargeType = ref("");
const pageNum = ref(1);
const pageSize = ref(10);
const total = ref(0);
const charge = ref({
  chargeType:""
})
/*批量删除数组数据*/
const multipleSelection = ref([]);
/*列表渲染数据*/
const tableData = ref([]);
const dialogFormVisible = ref(false);
const dialogTitle = ref("")
const getTableData = async () => {
  try {
    const res = await request.get('/chargeablePrice/getConsultantChargeRelations' ,{
      params: {
        pageNum:pageNum.value,
        pageSize:pageSize.value,
        chargeType:chargeType.value
      }
    })
    if (res.code === 200) {
      console.log(res.code)
      tableData.value = res.data.list
      total.value = res.data.total
    }
  }catch (e){
    ElMessage.error("列表渲染失败")
  }
}
/*重置按钮*/
const reset = ()=>{
  chargeType.value = "";
  getTableData();
}
/*可选择的收费类型*/
const chargeValue = ref();
const selectCharge = ref({});
const chargePrice = ref();
const consultantName = ref();
const selectConsultant = ref({});
const consultantChargeResult = ref({});
const selectChargeType = async () => {
  try {
    const res = await request.get('/charge/findAllChargeType')
    if (res.code === 200) {
      selectCharge.value = res.data
      console.log(selectCharge.value)
    }
  }catch (e){
    ElMessage.error("列表渲染失败")
  }
}
/*可选择的咨询师*/
const selectConsultantName = async () => {
  try {
    const res = await request.get('/consultant/findAllConsultant');
    if (res.code === 200) {
      selectConsultant.value = res.data;
      console.log(selectConsultant.value)
    }
  } catch (e) {
    ElMessage.error("列表渲染失败");
  }
}
/*新增*/
const add = () => {
  selectChargeType();
  selectConsultantName();
  chargeValue.value = ""
  consultantName.value = ""
  chargePrice.value = "";
  dialogTitle.value = "新增";
  dialogFormVisible.value = true;
}
const addCharge = async () => {
  consultantChargeResult.value = {
    chargeId: chargeValue.value,
    consultantId: consultantName.value,
    price: chargePrice.value
  }
  try {
    const res = await request.post("/chargeablePrice/add" , consultantChargeResult.value );
    if (res.code === 200){
      ElMessage.success("新增成功");
      getTableData();
    }else {
      ElMessage.error(res.msg);
      getTableData();
    }
  }catch (e){
    ElMessage.error("新增失败");
    dialogFormVisible.value = false;
  }
}
/*更新*/
const handleEdit = (row) => {
  consultantChargeResult.value = {
    ...row,
    chargeId: row.chargeId,
    consultantId: row.consultantId,
    price: row.price,
    id: row.id
  }
  console.log(row)
  chargeValue.value = row.chargeType;
  consultantName.value = row.consultantName;
  chargePrice.value = row.price;
  dialogTitle.value = "更新";
  dialogFormVisible.value = true;
}
const updateCharge = async () => {
  consultantChargeResult.value = {
    ...consultantChargeResult.value,
    price: chargePrice.value
  }
  try {
    const res = await request.put("/chargeablePrice/update" , consultantChargeResult.value);
    if (res.code === 200){
      ElMessage.success("更新成功");
      getTableData();
    }else {
      ElMessage.error(res.msg)
      getTableData();
    }
  }catch (e){
    ElMessage.error("更新失败")
  }
}
/*弹出层提交事件*/
const submit = async () => {
  console.log(chargeValue.value)
  console.log(consultantName.value)
  if (dialogTitle.value === "更新"){
    updateCharge();
  }else {
    addCharge();
  }
  dialogFormVisible.value = false;
}
/*弹出层取消事件*/
const cancel = () => {
  dialogFormVisible.value = false;
  getTableData();
}
/*删除*/
const handleDelete = async (row) => {
  const id = row.id;
  try {
    const res = await request.put(`/chargeablePrice/delete/${id}`);
    if (res.code === 200){
      ElMessage.success("删除成功");
      getTableData()
    }else {
      ElMessage.error(res.msg);
    }
  }catch (e){
    ElMessage.error("删除失败")
  }
}
/*批量删除*/
const handleSelectionChange = (val) => {
  multipleSelection.value = val
}
const delBatch = async () => {
  try {
    const res = await request.put("/chargeablePrice/deleteBatch" , multipleSelection.value);
    if (res.code === 200){
      ElMessage.success("批量删除成功");
      getTableData();
    }else {
      ElMessage.error(res.msg)
    }
  }catch (e){
    ElMessage.error("批量删除失败")
  }
}
/*批量导入*/
function successFileUpload(res) {
  if (res.code == 200){
    ElMessage.success("批量导入成功");
    getTableData();
  }
}
//批量导出
function exp(){
  location.href = "http://localhost:5173/api/charge/export";
}


/*分页*/
const handleSizeChange = (newSize) => {
  pageSize.value = newSize
  getTableData()
}
const handleCurrentChange = (newSize) => {
  pageNum.value = newSize
  getTableData()
}

onMounted(()=>{
  getTableData()
})

</script>

<template>
  <!--  <div class="page-content">
      <h2>系统用户管理</h2>
      <p>此处为系统用户管理页面，待实现具体功能。</p>
    </div>-->
  <div>

    <!--  搜索框  -->
    <div class="btns">
      <el-input placeholder="请输入收费类型" style="width: 240px ; margin-right: 10px" v-model="chargeType"></el-input>
      <el-button type="primary" @click="getTableData">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
      <el-button type="success" @click="add">新增</el-button>
      <el-button type="danger" @click="delBatch">批量删除</el-button>
      <el-upload
        v-if="false"
        action="http://localhost:5173/api/charge/upload"
        :on-success="successFileUpload"
        :show-file-list="false"
        style="display: inline-block; margin: 0 10px"
      >
        <el-button type="primary">批量导入</el-button>
      </el-upload>
      <el-button v-if="false" type="success" @click="exp()">批量导出</el-button>
    </div>

    <!--  table表格  -->
    <el-table
      ref="multipleTableRef"
      size="large"
      :data="tableData"
      row-key="id"
      border
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" :selectable="selectable" />
      <el-table-column property="id" label="ID" />
      <el-table-column property="chargeType" label="收费类型" />
      <el-table-column property="price" label="收费价格" />
      <el-table-column property="consultantName" label="咨询师" />
      <el-table-column label="操作" >
        <template #default="scope">
          <el-button type="primary" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--  弹出框  -->
    <el-dialog v-model="dialogFormVisible" :title="dialogTitle" width="500">
      <el-form :model="charge">
        <el-form-item v-if="dialogTitle == '新增'" label="收费类型" :label-width="formLabelWidth">
          <el-select
            v-model="chargeValue"
            collapse-tags
            placeholder="Select"
            style="width: 240px"
          >
            <el-option
              v-for="item in selectCharge"
              :key="item.id"
              :label="item.chargeType"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item v-if="dialogTitle == '更新'" label="收费类型" :label-width="formLabelWidth">
          <el-select
            v-model="chargeValue"
            collapse-tags
            placeholder="Select"
            style="width: 240px"
            disabled
          >
            <el-option
              v-for="item in selectCharge"
              :key="item.id"
              :label="item.chargeType"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="收费价格" :label-width="formLabelWidth">
          <el-input v-model="chargePrice" placeholder="请输入收费价格" />
        </el-form-item>
        <el-form-item v-if="dialogTitle == '新增'" label="咨询师" :label-width="formLabelWidth">
          <el-select
            v-model="consultantName"
            collapse-tags
            placeholder="Select"
            style="width: 240px"
          >
            <el-option
              v-for="item in selectConsultant"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item v-if="dialogTitle == '更新'" label="咨询师" :label-width="formLabelWidth">
          <el-select
            v-model="consultantName"
            collapse-tags
            placeholder="Select"
            style="width: 240px"
            disabled
          >
            <el-option
              v-for="item in selectConsultant"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="submit">
            确认
          </el-button>
        </div>
      </template>
    </el-dialog>

    <!--  分页  -->
    <el-pagination
      v-model:current-page="params.pageNum"
      v-model:page-size="params.pageSize"
      :page-sizes="[5, 10, 15, 20]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<style scoped>
.page-content {
  text-align: center;
  color: #666;
}

.page-content h2 {
  color: #1e3a8a;
  margin-bottom: 10px;
}
.btns{
  margin-bottom: 10px;
}
</style>
